﻿using DG.Tweening;
using UnityEngine;

namespace JQMergeFramework
{
    public class TrackUnit: MonoBehaviour
    {
        //绑定的塔吊
        private CityUnit mBindCity;

        private UIPreviewLvTips lvTips = null;
        public CityUnit BindCity
        {
            get { return mBindCity; }
        }

        private int mShowIndex = -1;

        public int showIndex
        {
            get { return mShowIndex; }
        }

        
        public void SetIndex(int index)
        {
            mShowIndex = index;
            lvTips?.RefreshCityState();
        }


        public void BindCityUnit(CityUnit city)
        {
            mBindCity = city;
            city.transform.SetParent(this.transform);
            this.transform.localRotation = Quaternion.Euler(Vector3.zero);
            this.transform.localScale = Vector3.one;
            city.transform.localRotation = Quaternion.Euler(new Vector3(0,0,0));
            
            lvTips = EffectPool.I.GetPoolObject(PoolName.PreviewLvTips).GetComponent<UIPreviewLvTips>();
            lvTips.BindCity(city);
        }

        public void SetParent(Transform parent,Vector3 pos)
        {
            this.transform.SetParent(parent);
            this.transform.position = pos;
            this.transform.localScale = Vector3.one;
            this.transform.localEulerAngles = Vector3.zero;
        }
        
        public void SetParent(Transform parent)
        {
            this.transform.SetParent(parent);
            this.transform.localScale = Vector3.one;
        }
        
        //删除绑定的履带
        public void PutTrackAnim()
        {
            mShowIndex = -1;
            mBindCity = null;
            TowerControl.I.RemoveTrackRecord(this);
            Sequence sq = DOTween.Sequence().SetId(this.transform.GetInstanceID());
            sq.Append(this.transform.DOLocalMoveX(this.transform.localPosition.x+15, GameCfg.DownTime));
            sq.Append(this.transform.DOLocalMoveY(this.transform.localPosition.y-30, GameCfg.DownTime));
            sq.OnComplete(() =>
            {
                EffectPool.I.PutPoolObject(PoolName.TrackTemplate,this.gameObject);
            });
            
            
            if (lvTips!=null)
            {
                lvTips.Clear();
                EffectPool.I.PutPoolObject(PoolName.PreviewLvTips,lvTips.gameObject);
            }
        }


        public void PutTrack()
        {
            mShowIndex = -1;
            mBindCity = null;
            TowerControl.I.RemoveTrackRecord(this);
            EffectPool.I.PutPoolObject(PoolName.TrackTemplate,this.gameObject);
            
            if (lvTips!=null)
            {
                lvTips.Clear();
                EffectPool.I.PutPoolObject(PoolName.PreviewLvTips,lvTips.gameObject);
            }
        }


    }
}